package org.happen.android.widget;

import org.liushui.mycommons.android.annotation.ViewInject;
import org.liushui.mycommons.android.annotation.helper.InjectHelper;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.happen.android.R;

public class NormalDialog implements View.OnClickListener
{
	Context context;
	Dialog dialog;
	OnDialogCalback onDialogCalback;

	View contentView;
	@ViewInject(id = R.id.btn_dialog_ok)
	Button btnOk;
	@ViewInject(id = R.id.btn_dialog_cancel)
	Button btnCancnel;
	@ViewInject(id = R.id.tv_dialog_title)
	TextView tvTitle;
	@ViewInject(id = R.id.tv_dialog_message)
	TextView tvMessage;

	String titleText, messageText;
	String okText, cancelText;

	public NormalDialog(Context context)
	{
		this.context = context;
	}

	void init()
	{
		contentView = View.inflate(context, R.layout.dialog_normal, null);
		InjectHelper.init(this, contentView);
		btnCancnel.setOnClickListener(this);
		btnOk.setOnClickListener(this);
	}

	void setValues()
	{
		tvTitle.setText(titleText);
		tvMessage.setText(messageText);

		if (okText != null)
		{
			btnOk.setText(okText);
		}
		if (cancelText != null)
		{
			btnCancnel.setText(cancelText);
		}
	}

	public Dialog create()
	{
		init();

		dialog = new Dialog(context, R.style.AppTheme_Dialog_NoTitleBar);
		dialog.setContentView(contentView);

		setValues();

		return dialog;
	}

	public void setContentText(String title, String message)
	{
		this.titleText = title;
		this.messageText = message;
	}

	/**
	 * 设置按钮显示，使用null，则显示默认(确定或者取消 )
	 * 
	 * @ReqParam ok
	 * @ReqParam cancel
	 */
	public void setButtonText(String ok, String cancel)
	{
		this.okText = ok;
		this.cancelText = cancel;
	}

	public void onClick(View v)
	{
		if (v == btnOk)
		{
			if (onDialogCalback != null)
			{
				onDialogCalback.onOk(this);
			}
		} else if (v == btnCancnel)
		{
			if (onDialogCalback != null)
			{
				onDialogCalback.onCancel(this);
			}
		}
	}

	public Dialog getDialog()
	{
		return dialog;
	}

	public void setOnDialogCalback(OnDialogCalback onDialogCalback)
	{
		this.onDialogCalback = onDialogCalback;
	}

	public interface OnDialogCalback
	{
		public void onOk(NormalDialog dialog);

		public void onCancel(NormalDialog dialog);
	}
}